home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7659 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  59 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.netins.net!isac!gg
  3. From: gg@isac.hces.com (Greg Goodrich)
  4. Subject: Re: more problems with qsort
  5. Message-ID: <1996Feb27.235558.3751@isac.hces.com>
  6. Organization: Health Care Expert Systems
  7. X-Newsreader: TIN [version 1.2 PL2]
  8. References: <177399702S86.JW1675A@american.edu>
  9. Date: Tue, 27 Feb 1996 23:55:58 GMT
  10.  
  11. James D. Watson (JW1675A@american.edu) wrote:
  12. : Hi folks --
  13. :  
  14. : after reading the FAQ entries about qsort, I thought I had my problem
  15. : licked, but it seems not.
  16. :  
  17. : Architecture: SunOS 4.1.3 with ANSI-C compiler.
  18. :  
  19. : I'm doing some file manipulation and between steps A and B, I need to sort.
  20. : For various reasons, I don't want to popen() to the sort utility--I want to
  21. : use qsort.  Here's what I'm doing:
  22. :  
  23. :    get number of lines in the file
  24. :    (char**)malloc with enough room for all lines
  25. :    for each line in the file {
  26. :      (char*)malloc(90)  /* 90 is enough room for each line in file */
  27. :      copy each line into the newly malloc()ed space
  28. :      point a (char**) to the newly malloc()ed space
  29. :    }
  30. :  
  31. : so now I should have "lines" number of pointers to pointers to char,
  32. : each one pointing to 90 bytes containing a line in the file.
  33. :  
  34. : So, I call qsort(array[0], lines, 90, compare)
  35. : where compare is my comparison function -- prepared as discussed
  36. : in the FAQ.  Now all I get are core dumps during the call to qsort().
  37. : :-)
  38. :  
  39. : The core dump isn't in my compare routine -- it's somewhere else in
  40. : the qsort() function according to dbx -- so I'm sure it's a problem
  41. : setting up my data.
  42.  
  43. I would suggest making sure that you are actually getting the data into
  44. the malloc'd space the way that you think you are.  It sounds like you
  45. are passing an invalid reference pointer "array[0]" to qsort.  I am
  46. assuming that array[0] is a pointer to char, which is the first input
  47. line from the file.  Make sure that this is actually what is there by
  48. debugging the program and printing the value before the qsort.  Or you
  49. could place a print in front of the qsort, which accomplishes the same
  50. thing.  If qsort gets passed an invalid ptr as the first argument, it
  51. will core dump.
  52.  
  53. Greg.
  54. -- 
  55. _______________________________________
  56.     Greg Goodrich - gg@hces.com
  57.     Software Engineer
  58.     PACE Health Management Systems
  59.